﻿using System;
using System.Windows.Forms;
using DevelopTool.Bll.TipsBlls;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.CommonForms;
using DevelopTool.Model.TipsModels;
using Sunny.UI;

namespace DevelopTool.Forms.TipsForms
{
    public partial class TipsForm : BaseUIPage
    {
        private Tips tips;

        private TipsBll tipsBll;

        public TipsForm(Tips tips)
        {
            InitializeComponent();

            this.tips = tips;
            this.tipsBll = new TipsBll();
        }


        private void TipsForm_Load(object sender,EventArgs e)
        {
            this.rTxtTipsText.Text = this.tips.Text;
            this.txtTipsCron.Text = this.tips.Cron;
            this.txtAudioPath.Text = this.tips.AudioPath;
            this.cboAudioPlay.Checked = this.tips.PlayAudio;

            SetBtnTextByRunState();

            ShowRunDetail(this.txtTipsCron.Text);
        }


        /// <summary>
        /// 根据运行状态设置按钮文本和图标
        /// </summary>
        private void SetBtnTextByRunState()
        {
            if(this.tipsBll.IsRun(this.tips))
            {
                this.btnRun.Symbol = 61517;
                this.btnRun.Text = "停止";
            }
            else
            {
                this.btnRun.Symbol = 61515;
                this.btnRun.Text = "运行";
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender,EventArgs e)
        {
            Save();
        }

        private void Save()
        {
            if(string.IsNullOrEmpty(this.txtTipsCron.Text))
            {
                this.ShowWarningNotifier("请填写提醒规则Cron");
                return;
            }

            this.tips.AudioPath = this.txtAudioPath.Text;
            this.tips.PlayAudio = this.cboAudioPlay.Checked;
            this.tips.Cron = this.txtTipsCron.Text;
            this.tips.Text = this.rTxtTipsText.Text;
            this.tips = this.tipsBll.Save(this.tips);

            this.ShowSuccessNotifier("保存成功");

            //如果保存时任务正在运行，那么就重新启动下任务
            if(this.tipsBll.IsRun(this.tips))
            {
                tipsBll.TipsRun(this.tips);
            }
        }


        /// <summary>
        /// 运行/停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRun_Click(object sender,EventArgs e)
        {
            if(this.tipsBll.IsRun(this.tips))//停止
            {
                var tuple = tipsBll.TipsStop(this.tips);
                if(tuple.Item1)
                {
                    this.ShowSuccessNotifier(tuple.Item2);

                    tips.IsRun = false;//设置任务状态为停止运行
                    Save();
                }
                else
                {
                    this.ShowWarningNotifier(tuple.Item2);
                }
            }
            else//运行
            {
                var tuple = tipsBll.TipsRun(this.tips);
                if(tuple.Item1)
                {
                    this.ShowSuccessNotifier(tuple.Item2);

                    tips.IsRun = true;//设置任务状态为正在运行
                    Save();
                }
                else
                {
                    this.ShowWarningNotifier(tuple.Item2);
                }
            }

            SetBtnTextByRunState();
        }


        /// <summary>
        /// 打开Cron选择窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtTipsCron_ButtonClick(object sender,EventArgs e)
        {
            QuartzCronForm quartzCronForm = new QuartzCronForm(this.txtTipsCron.Text);
            if(quartzCronForm.ShowDialog() == DialogResult.OK)
            {
                this.txtTipsCron.Text = quartzCronForm.CronText;

                ShowRunDetail(this.txtTipsCron.Text);
            }
        }

        /// <summary>
        /// 展示最近50次运行详情
        /// </summary>
        private void ShowRunDetail(string cron)
        {
            if(string.IsNullOrEmpty(cron)) return;

            //最近50次运行
            this.listBoxLastRunDetail.Items.Clear();
            var timeList = QuartzHelper.GetTaskeFireTime(cron);
            foreach(string time in timeList)
            {
                this.listBoxLastRunDetail.Items.Add(Convert.ToDateTime(time).ToString("yyyy-MM-dd HH:mm:ss dddd"));
            }
        }

        /// <summary>
        /// 选择音频文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtAudioPath_ButtonClick(object sender,EventArgs e)
        {
            using(OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Title = "请选择音频文件";
                openFileDialog.Filter = "音频|*.wav;";
                if(openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    this.txtAudioPath.Text = openFileDialog.FileName;
                }
            };
        }
    }
}
